package tcln.sors.model.core.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import tcln.sors.model.core.dom.Entity;
import ch.elca.el4j.services.persistence.generic.dao.impl.FallbackDaoRegistry;

/**
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public class DaoFactoryImpl extends FallbackDaoRegistry implements DaoFactory {

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public <D extends Dao<T>, T extends Entity<Integer>> D getDaoFor(Class<T> entityType, Class<D> desireDaoType) {
        return (D) super.getFor(entityType);
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    @Override
    public <T extends Entity<Integer>> Dao<T> getDaoFor(Class<T> entityType) {
        return getDaoFor(entityType, Dao.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void flushSession() {
        Session session = this.m_applicationContext.getBean("sessionFactory", SessionFactory.class).getCurrentSession();
        session.flush();
    }
}
